//Page Object
var QQMapWX = require('../../lib/qqmap-wx-jssdk.js');
var qqmapsdk;
const chooseLocation = requirePlugin('chooseLocation');
var user = require('../../utils/user');
var util = require('../../utils/util');
var api = require('../../request/api');
const app = getApp(); // 引入app
const mapKey = app.globalData.qqmap.key;
import Dialog from '../../vant/weapp/dialog/dialog';
import Toast from '../../vant/weapp/toast/toast';

Page({
  data: {
    showShade:false,
    start_lo: '',
    end_lo: '',
    is_start: '',
    is_end: '',
    start_det: '',
    end_det: '',
    currentDate: '',
    time_show: false,
    nowTime: new Date().getTime(),
    nowTime_long: '',
    timeFormat:'yyyy-mm-dd HH:MM:SS',
    st_loc: '',
    end_loc: '',
    st_time: '',
    pin: '',
    number: '',
    remake: '',
    sms: '',
    formatNowTime:'',
    code:'',
    answer:'',
    uuid:''
  },
  //options(Object)
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: mapKey
    });
  },
  onReady: function () {
    this.getCode()
  },
  onShow: function () {

  },
  alert(title, msg) {
    Dialog.confirm({
        title: title,
        message: msg,
      })
      .then(() => {
        // on confirm
      })
      .catch(() => {
        // on cancel
      });
  },
  onHide: function () {

  },
  onUnload: function () {
    chooseLocation.setLocation(null);
    user_location = '';
  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  },
  onPageScroll: function () {

  },
  //item(index,pagePath,text)
  onTabItemTap: function (item) {},
  whereAreYou: function (_location) {
    wx.getLocation({
      type: "gcj02",
      success(user_location) {
        console.log(user_location)
        var longitude = user_location.longitude;
        var latitude = user_location.latitude;
        var location = JSON.stringify({
          longitude: longitude,
          latitude: latitude
        });
        // let location = JSON.stringify({}) : _location;
        let key = mapKey; //使用在腾讯位置服务申请的key
        let referer = '选择位置'; //调用插件的app的名称
        let category = '生活服务,娱乐休闲';
        wx.navigateTo({
          url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&location=' + location + '&category=' + category
        });
      }
    })
  },
  whereAreYouGoing: function (_location) {
    let key = mapKey; //使用在腾讯位置服务申请的key
    let referer = '一起走'; //调用插件的app的名称
    //终点
    let endPoint = JSON.stringify({
      'name': '吉野家(北京西站北口店)',
      'latitude': 39.89631551,
      'longitude': 116.323459711
    });
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
    })
  },
  checkMap: function (e) {
    var a = e.currentTarget.dataset.type;
    console.log(a);
    if (a == 0) {
      this.setData({
        is_start: true,
        is_end: false
      })
    } else if (a == 1) {
      this.setData({
        is_start: false,
        is_end: true
      })
    }
    this.whereAreYou();
  },

  // submitFrom:function(e){
  //   console.log(e.detail.value)
  //   console.log(e.detail.value.startLoc)
  //   console.log(e.detail.value.endLoc)
  //   console.log(e.detail.value.startTime)
  //   console.log(e.detail.value.platform)
  //   console.log(e.detail.value.number)
  //   console.log(e.detail.value.remark)
  //   console.log(e.detail.value.code)
  //   if(st == '' || st == null || end == null || end == ''){
  //     return  wx.showModal({
  //       title: '提示',
  //       content: '当前没有输入起始地或者目的地',
  //       success: function(res) {
  //         if (res.confirm) {
  //         console.log('用户点击确定')
  //         } else if (res.cancel) {
  //         console.log('用户点击取消')
  //         }
  //       }
  //     })
  //   }
  //   wx.showModal({
  // 		title: '提示',
  // 		content: '出发地点为:'+ st + ',目的地为：'+ end,
  // 		success: function(res) {
  // 			if (res.confirm) {
  // 			console.log('用户点击确定')
  // 			} else if (res.cancel) {
  // 			console.log('用户点击取消')
  // 			}
  // 		}
  // 	})

  // },

  submitFrom: function (e) {
    var info = {};
    info.endLocation = this.data.end_loc;
    info.startLocation = this.data.st_loc;
    info.time = this.data.formatNowTime;
    info.peopleNumber = this.data.number;
    info.pin = this.data.pin;
    info.remark = this.data.remake;
    info.uuid = this.data.uuid;
    info.code = this.data.sms;
    console.log(info);
    if(this.data.sms != this.data.answer){
      app.alertFail("验证码错误")
    }
    info.createManId = wx.getStorageSync('userInfo').userId;
    // info.createMan = wx.getStorageSync('userInfo').realName;
    info.createMan = '123123';
    
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '创建中...',
      forbidClick: true,
    });
    util.post(api.order.append,info).then( res => {
      Toast.clear();
      this.setData({ showShade: false });
      if(res.code == 200){
        app.alertSuccess('创建成功');
      }else{
        app.alertFail(res.message)
      }
      console.log(res)
    }).catch(()=>{
      Toast.clear();
      this.setData({ showShade: false });
    });
  },
  // onInput_time(event) {
  //   this.data.st_time = util.dateFormat(, new Date(event.detail));
  // },
  onClose() {
    this.setData({
      time_show: false
    });
  },
  showTimeBox(){
    this.setData({
      time_show: true
    });
  },
  checkTime(){
    this.setData({
      formatNowTime:util.dateFormat(this.data.timeFormat,new Date(this.data.nowTime_long))
    })
  },
  onInput(event) {
    this.setData({
      nowTime_long: event.detail,
    });
  },
  getCode(){
    util.get(api.code,{}).then(res =>{
      console.log(res)
      var answer = res.message.slice(res.message.indexOf(':') + 1);
      var uuid = res.data.uuid;
      var code = res.data.img;
      this.setData({
        code:code,
        uuid:uuid,
        answer:answer
      })
      console.log(this.data);
    })
  },
});